/**
 * 413.等差数列划分
 */
public class Exerciser5 {
    public int numberOfArithmeticSlices(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        // 初始化
        int ret = 0;
        for(int i = 2;i < n;i++) {
            // 等差中项表达式：nums[i] + nums[i - 2] == 2*nums[i - 1]
            dp[i] = (nums[i] + nums[i - 2] == 2*nums[i - 1] ? dp[i - 1] + 1 : 0);
            ret += dp[i];
        }
        return ret;
    }
}
